/**
 * 
 */
var EnterpriseForm = {
	init: function(){
		EnterpriseForm.initEnterpriseForm();
		EnterpriseForm.initDefauleValue();
	},
	initDefauleValue: function(){
		var provinceValue = $("#province").val();
		$("#province").val(provinceValue).css("color","#999");      
		$("#province").focus(function(){       
			 if ($("#province").val() == provinceValue) {         
				 $("#province").val("").css("color","#000");//输入值的颜色      
				 }     
			 }).blur(function() {      
				 if ($("#province").val() == "") {        
					 $("#province").val(provinceValue).css("color","#999");//默认值的颜色      
					 }     
				 });
		var cityValue = $("#city").val();  
		$("#city").val(cityValue).css("color","#999"); 
		$("#city").focus(function(){       
			 if ($("#city").val() == cityValue) {         
				 $("#city").val("").css("color","#000");//输入值的颜色      
				 }     
			 }).blur(function() {      
				 if ($("#city").val() == "") {        
					 $("#city").val(cityValue).css("color","#999");//默认值的颜色      
					 }     
				 });  
	},
/**
	 * 保存业户信息
	 */
	saveEnterprise: function() {
		var flag = true;
		//获取车辆ID,在修改时当前车辆的自编号在不修改的情况下依旧可以保存
		var vid =$("#id").val();
		
		//1.业户名判断	
		var enterpriseName = $.trim($("#name").val());
		var regEnterprise = enterpriseName.length;
		if(regEnterprise >30 || regEnterprise <= 2){
			global.ajaxLoading.pop("业户名称长度必须为3-30",false,2000);
			flag = false;
			return;
		}else{
		//判断是否存在重复的业户名
			$.ajax({
				type : 'post',
				async : false,
				url : appCtx + "/base/enterprise/queryEnterpriseByName.html?enterpriseName="+enterpriseName,
				cache : false,
				dataType : "JSON",
				success : function(data) {
					if (data.enterpriseId != 0) {
						var id =$("#id").val();
						if (vid != data.enterpriseId) {
							global.ajaxLoading.pop("业户名已经存在,请重新填写",false,2000);
							//showVelidateModal("业户名已经存在,请重新填写");
							flag = false;
							return;
						}
					}
				}
			});
		};
		
		//2.接入平台判断(暂时不效验其为空)
		var platformId = $("#platformId").val();
		if(platformId == ""){
				flag = false ;
				global.ajaxLoading.pop("请选择接入平台",false,2000);
				return;
		}
		
		//3.所属区域判断(暂时不效验其为空)
		var areaId = $("#areaId").combotree('getValue');
		if(areaId == "" || typeof(areaId) == 'undefined'){
				flag = false ;
				global.ajaxLoading.pop("请选择所属区域",false,2000);
				return;
		}
		//4.经营范围判断
/*		var bizScopeId = $("#bizScopeId").val();
		if(bizScopeId == ""){
				flag = false ;
				showVelidateModal("经营范围不能为空");
				return;
		}*/
		// 5.重复的经营许可证号判断
		var bizLicense = $.trim($("#bizLicenseNum").val());
		var regBizLicense = /^[0-9]{3,20}$/;
		if(!(regBizLicense.test(bizLicense))){
			flag = false ;
			showVelidateModal("经营许可证号必须以3-20位数字！");
			return;
		}
		//else{
//			$.ajax({
//				type : 'post',
//				async : false,
//				url : appCtx + "base/enterprise/queryEnterpriseByBizLicenseNum.html?bizLicenseNum="+bizLicense,
//				cache : false,
//				dataType : "JSON",
//				success : function(data) {
//					if (data.code != 0) {
//						flag = false ;
//						showVelidateModal("经营许可证号名已经存在,请重新填写！");
//						return;
//					}	
//				}
//			});
//		}	
		//6.发证机关判断(暂时不效验其为空)

		//7.证件有效日期判断
/*		var inputStartDate =  $("#invalidStartTime").val();
		var inputEndDate =  $("#invalidEndTime").val();
		if(inputEndDate == ""|| inputEndDate == undefined || inputEndDate =="" || inputEndDate == undefined){
			flag = false ;
			showVelidateModal("证件有效日期不能为空！");
			return;
		}else{
			var startTime = Date.parse(new Date(inputStartDate));
			var endTime = Date.parse(new Date(inputEndDate));
			if (startTime > endTime) {
				flag = false ;
				showVelidateModal("证件有效日期有误！");
				return;
			}
		}*/
		//8.法人代表判断
/*		var corporate =  $("#corporate").val();
		var regCorporate = enterpriseName.length;
		if(regCorporate > 20 || regCorporate < 2){
			showVelidateModal("长度必须为2-20！");
			flag = false;
			return;
		}*/
		
		//9.联系电话判断
/*		var phone = $("#phone").val();
		var reg = /^1(3|4|5|7|8)\d{9}$/;
		if (phone != "") {
			if(!(reg.test(phone))){
				showVelidateModal("请输入正确的电话号码!");
				flag = false;
				return;
			}
		}*/
		
		//10.系统采集密码判断
/*		var sysPwd = $.trim($("#sysPwd").val());
		var regSys = /^[a-zA-Z0-9]{5,20}$/;
		if (sysPwd == "") {
			showVelidateModal("系统采集密码不能为空!");
			flag = false;
			return;
		}else if(!(regSys.test(sysPwd))){
			showVelidateModal("系统采集密码为数字和字母组成!");
			flag = false;
			return;
		};*/
		if (flag) {
			// 保存信息
			$.ajax({
				type : 'post',
				url : appCtx + "base/enterprise/saveEnterpriseForm.html",
				data : $("#enterpriseForm").serialize(),
				cache : false,
				dataType : "JSON",
				success : function(data) {
					if (data.code == "0") {
						global.ajaxLoading.pop('操作成功!',false,2000);
						closeDataDialog();
						var funStr = "window.frames['" + mFrameName + "']."+ mDialogCallback;
						eval("(" + funStr + ")");
					}else if (data.code == "1") {
						global.ajaxLoading.pop("操作失败,原因是已经存在相同许可证号!",false,2000);
						return;
					}  else {
						global.ajaxLoading.pop("操作失败!",false,2000);
						return;
					}
					
				}
			});
		}
	},
	
	detailConfirm: function(){
		closeDataDialog(); //关闭modal框
		$("#tbEnterprise").flexOptions({
			url : appCtx + "base/enterprise/loadEnterpriseList.html",
		}).flexReload();
	},
	/**
	 * 初始化新增列表
	 */
	initEnterpriseForm: function(){
		if(action=='insert'){
			//$('#enterpriseId').combobox();
		}else{
			var enterpriseId = $("#id").val();
    		//获取终端
    		//EnterpriseForm.checkTerminalsByWorkUnitID(UnitID);
		}
		
		//获取区域列表
		$('#areaId').combotree({
			url:appCtx + "/system/common/queryAreaTree.html",
			valueField: 'id',
            textField: 'text',
            method: 'post',
            collapseAll : true,
            editable:false,
            onSuccess : function(data){
            	
            },            
            loadFilter: function(data){
            	 data.unshift({ id: '0', text: '请选择所属区域', selected: "selected" });
                 return data;
    		},
    		onBeforeSelect:function(node){
    		
   			 var rows = node.children; 
   			  if(rows.length>1){
   				  $("#queryArea").tree("unselect");
   			  } 
   		  }, 
    		onLoadSuccess: function (data) {
    			$('#areaId').combotree('tree').tree("collapseAll");
    			$('#areaId').combotree('setValue','0')
         	}
			
		});
		//获取平台列表
		$.ajax({
            type: 'post',
            url: appCtx + "/base/enterprise/loadPlatformList.html?time="+ new Date().getTime(),
            cache: false,
            dataType: "JSON",
            success: function (data) {			
    			if(data){
    				var platform = $("#platform").val();
    				var selectStr="";
    				$.each(data,function(entryIndex,entry) {
    					if (platform == entry.platformId) {
    						selectStr+="<option value='"+entry.platformId+"' selected='selected'>"+entry.platformName+"</option>";
						}else{
							selectStr+="<option value='"+entry.platformId+"'>"+entry.platformName+"</option>";
						}
    					
    				});
    				
    				$('#platformId').html(selectStr);
    				
    				$("#platformId").select2();
    			}
            }
        });
		//获取经营范围列表
		$.ajax({
            type: 'post',
            url: appCtx + "/base/enterprise/loadBizScopeList.html?time="+ new Date().getTime(),
            cache: false,
            dataType: "JSON",
            success: function (data) {			
    			if(data){	
    				var selectStr="";
    				var bizScope =  $("#bizScope").val();
    				$.each(data,function(entryIndex,entry) {
    					if (bizScope == entry.id) {
    						selectStr+="<option value='"+entry.id+"' selected='selected'>"+entry.biz_name+"</option>";
						}else{
							selectStr+="<option value='"+entry.id+"'>"+entry.biz_name+"</option>";
						}
    				});
    				$('#bizScopeId').html(selectStr);
    				
    				$("#bizScopeId").select2();
    			}
            }
        });
		//获取发证机关列表
		$.ajax({
            type: 'post',
            url: appCtx + "/base/enterprise/loadLssuingAuthList.html?time="+ new Date().getTime(),
            cache: false,
            dataType: "JSON",
            success: function (data) {			
    			if(data){
    				var selectStr="";
    				var lssuingAuth =  $("#lssuingAuth").val();
    				selectStr+="<option value=''>请选择发证机关</option>";
    				$.each(data,function(entryIndex,entry) {
    					if (lssuingAuth == entry.id) {
    						selectStr+="<option value='"+entry.id+"' selected='selected'>"+entry.orgnName+"</option>";
						}else{
							selectStr+="<option value='"+entry.id+"'>"+entry.orgnName+"</option>";
						}
    				});
    				
    				$('#lssuingAuthId').html(selectStr);
    				
    				$("#lssuingAuthId").select2();
    			}
            }
        });
		//获取经济类型列表
		$.ajax({
            type: 'post',
            url: appCtx + "/base/enterprise/loadEconomyTypeList.html?time="+ new Date().getTime(),
            cache: false,
            dataType: "JSON",
            success: function (data) {			
    			if(data){		
    				var selectStr="";
    				var economy_type =  $("#economy_type").val();
    				$.each(data,function(entryIndex,entry) {
    					if (economy_type == entry.id) {
    						selectStr+="<option value='"+entry.id+"' selected='selected'>"+entry.name+"</option>";
						}else{
							selectStr+="<option value='"+entry.id+"'>"+entry.name+"</option>";
						}
    				});
    				
    				$('#economy_type_id').html(selectStr);
    				
    				$("#economy_type_id").select2();
    			}
            }
        });
	},
};

EnterpriseForm.init();
//校验业户名
function validateName (){
	var enterpriseName = $.trim($("#name").val());
	var reg = enterpriseName.length;
	var msg = document.getElementById("namemsg")
	if(reg>=20 || reg<=2){
		msg.innerHTML = "长度为3-20！";
		msg.style.color = "red";
	}else{
	//判断是否存在重复的业户名
		$.ajax({
			type : 'post',
			async : false,
			url : appCtx + "/base/enterprise/queryEnterpriseByName.html?enterpriseName="+enterpriseName,
			cache : false,
			dataType : "JSON",
			success : function(data) {
				if (data.enterpriseId != 0) {
					msg.innerHTML ="已经存在！";
					msg.style.color = "red";
					
				}else{
					msg.innerHTML ="可以使用！";
					msg.style.color = "green";
				}
			}
		});
	}
};
